﻿using NFramework.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NFramework.UnitTests.ObjectModelTests
{
    /// <summary>
    ///This is a test class for AutoInstanceTest and is intended
    ///to contain all AutoInstanceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AutoInstanceTest
    {
        [TestMethod()]
        public void InitialAutoInstanceHasNoInstance()
        {
            Assert.IsFalse(new AutoInstance<GenericParameterHelper>().HasInstance);
            Assert.IsFalse(new AutoInstance<IMockData, MockData>().HasInstance);
        }

        [TestMethod()]
        public void AutoInstanceHasNoInstanceUntilInstanceIsCalled()
        {
            var target1 = new AutoInstance<GenericParameterHelper>();
            Assert.IsFalse(target1.HasInstance);
            target1.Instance.Data = 0;
            Assert.IsTrue(target1.HasInstance);

            var target2 = new AutoInstance<IMockData, MockData>();
            Assert.IsFalse(target2.HasInstance);
            target2.Instance.Data = 0;
            Assert.IsTrue(target2.HasInstance);
        }

        [TestMethod()]
        public void AutoInstanceHasNoInstanceAfterSetToNull()
        {
            var target1 = new AutoInstance<GenericParameterHelper>();
            target1.Instance.Data = 0;
            Assert.IsTrue(target1.HasInstance);
            target1.Instance = null;
            Assert.IsFalse(target1.HasInstance);

            var target2 = new AutoInstance<IMockData, MockData>();
            target2.Instance.Data = 0;
            Assert.IsTrue(target2.HasInstance);
            target2.Instance = null;
            Assert.IsFalse(target2.HasInstance);
        }

        [TestMethod()]
        public void AutoInstanceHoldsInstanceAfterBeingSet()
        {
            var data = System.Guid.NewGuid().GetHashCode();
            var target1 = new AutoInstance<GenericParameterHelper>();
            target1.Instance.Data = data;
            Assert.AreEqual(data, target1.Instance.Data);

            var target2 = new AutoInstance<IMockData, MockData>();
            target2.Instance.Data = data;
            Assert.AreEqual(data, target2.Instance.Data);
        }

        interface IMockData
        {
            int Data { get; set; }
        }

        class MockData : GenericParameterHelper, IMockData
        {
            #region IMockData Members

            int IMockData.Data
            {
                get
                {
                    return base.Data;
                }
                set
                {
                    base.Data = value;
                }
            }

            #endregion
        }

    }
}
